import { Bus, CONN_ACTIVITY, CONN_BIG_AMOUNT, CONN_CHAIN_CLEVER, CONN_FLUCTUATION, CONN_STABLE_CURRENCY, CONN_STABLE_EVENT } from "../connetion/Bus";
import { IBroadcast } from "./IBroadcast";

export class ActivityChainBroadcast extends IBroadcast {
    // channelParam: any = {"event":"subscribe","symbol":"all","topic":"on_chain","interval":"S1"};
    channelParam: any[] = [
        { event: "subscribe", group: "0x", topic: "ON_CHAIN" },
        { event: "subscribe", group: "ALL", topic: "on_chain" },
        { event: "subscribe", group: "BIG_AMOUNT", topic: "ON_CHAIN" },

        { event: "subscribe", group: "CAPITAL_ABNORMAL", topic: "ON_CHAIN" }

        // { event: "subscribe", group: "FLUCTUATION", topic: "ON_CHAIN" },
    ];

    changeParam(symbol: string) {
        this.unSubscribe();
        this.channelParam[0].group = symbol;
        this.subscribe();
    }

    subscribe(): void {
        this.unSubscribe();
        this.channelParam.forEach(i => {
            console.log("###jsz 订阅", JSON.stringify(i));
            this.chainSend(JSON.stringify(i));
        });
    }

    unSubscribe(): void {
        this.channelParam.forEach(i => {
            const obj = { ...i };
            obj.event = "unsubscribe";
            // this.socket.chainSend(JSON.stringify(obj));
            console.log("###jsz 取消订阅", JSON.stringify(obj));
            this.chainSend(JSON.stringify(obj));
        });
    }

    handle(jsondata: any): void {
        // console.warn("chain act", jsondata);
        if (!jsondata.data) return;
        // if( jsondata.data.type == "swap" && (jsondata.data.outAmount || jsondata.data.inAmount )){
        //     return;
        // }

        switch (jsondata.group) {
            case "CAPITAL_ABNORMAL":
                // console.log("CAPITAL_ABNORMAL", jsondata.group);
                if (jsondata.data.type !== "cex") {
                    Bus.emit(CONN_CHAIN_CLEVER, jsondata.data);
                }
                break;
            case "ALL":
                // console.log("ALL 鏈上");
                Bus.emit(CONN_ACTIVITY, jsondata.data);
                break;
            case "FLUCTUATION":
            // console.log("FLUCTUATION 成交");
            // Bus.emit(CONN_FLUCTUATION, jsondata.data);
            // break;
            case "BIG_AMOUNT":
                // console.log("BIG_AMOUNT 大额转账");
                Bus.emit(CONN_BIG_AMOUNT, jsondata.data);
                // Bus.emit(CONN_STABLE_CURRENCY, jsondata.data);
                break;
            default:
                // if(jsondata.group == this.channelParam[0].group){
                Bus.emit(CONN_STABLE_CURRENCY, jsondata.data);
                if (jsondata.group.indexOf("BIG_AMOUNT:") != -1) {
                    Bus.emit(CONN_STABLE_EVENT, jsondata.data);
                }
                // }
                break;
        }
    }
}
